<?php
class IndexControl extends AdminModule
{
	//全局不使用布局
	public $layout = false;

	public function actionIndex()
	{
		//后台首页使用布局
		$this->layout = 'main';
		$this->render('home');
	}

	/**
	 * 后台登陆
	 */
	public function actionLogin()
	{
		if (!$this->user->isGuest()) {
			header('Location:'.D::createUrl('admin/index'));
			exit;
		}
		$encrypt_key  = D::$config['encrypt'];
		if (isset($_POST['token'])) {
			if ($this->encrypt->decode($_POST['token'], $encrypt_key . '_login') != 'login_auth') 
				$this->error($msg);
			D::import('admin:component.rootIdentity');
			$username = trim(htmlspecialchars($_POST['username']));
			$password = trim(htmlspecialchars($_POST['password']));
			$Identity = new rootIdentity($username, $password);
			if ($Identity->authenticate()) {
				$this->user->login($Identity);
				$this->session->set('admin_site_id', 1);
				$this->ajaxReturn( $this->ajaxInfo(1, '', D::createUrl('admin')) );
			} else {
				$this->ajaxReturn( $this->ajaxInfo(0, '用户名或密码错误') );
			}
		}
		$var = array(
			'token_code' => $this->encrypt->encode('login_auth', $encrypt_key.'_login'),
		);
		$this->render('login', $var);
	}

	/**
	 * 注销
	 */
	public function actionlogout()
	{
		$this->user->logout();
		$this->session->destory();
		header('Location:'.D::createUrl('admin/index/login'));
	}

	/**
	 * 后台框架页
	 */
	public function actionFrame($id = 0, $url = '')
	{
		if ($id === 0 && $url === '') {
			$this->render('frame');
		} else {
			$html = $this->render('frame', null, true);
			$this->ajaxReturn( array('html'=>$html, 'id'=>$id, 'url'=>D::createUrl($url)) );
		}

	}

	/**
	 * 选择站点
	 */
	public function actionSelectSite($site_id = 1)
	{
		$this->session->set('admin_site_id', (int)$site_id);
		exit;
	}
}
?>